import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
//232 - 用栈实现队列
class MyQueue {
    Stack<Integer> stack1;
    Stack<Integer> stack2;
    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int x) {
        if(!stack1.empty()){
            stack1.push(x);
        }else if(!stack2.empty()){
            stack2.push(x);
        }else {
            stack1.push(x);
        }
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(!stack1.empty()){
            int size = stack1.size();
            for (int i = 0; i < size - 1; i++) {
                stack2.push(stack1.pop());
            }
            int tmp = stack1.pop();
            for (int i = 0; i < size - 1; i++) {
                stack1.push(stack2.pop());
            }
            return tmp;
        }else {
            int size = stack2.size();
            for (int i = 0; i < size - 1; i++) {
                stack1.push(stack2.pop());
            }
            int tmp = stack2.pop();
            for (int i = 0; i < size - 1; i++) {
                stack2.push(stack1.pop());
            }
            return tmp;
        }
    }

    public int peek() {
        if(empty()){
            return -1;
        }
        if(!stack1.empty()){
            int size = stack1.size();
            for (int i = 0; i < size; i++) {
                stack2.push(stack1.pop());
            }
            int tmp = stack2.peek();
            for (int i = 0; i < size; i++) {
                stack1.push(stack2.pop());
            }
            return tmp;
        }else {
            int size = stack2.size();
            for (int i = 0; i < size; i++) {
                stack1.push(stack2.pop());
            }
            int tmp = stack1.peek();
            for (int i = 0; i < size; i++) {
                stack2.push(stack1.pop());
            }
            return tmp;
        }
    }

    public boolean empty() {
        if(stack1.size() == 0 && stack2.size() == 0){
            return true;
        }
        return false;
    }
}
//225 - 用队列实现栈
class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if(queue1.size() != 0){
            queue1.offer(x);
        }else if(queue2.size() != 0){
            queue2.offer(x);
        }else {
            queue1.offer(x);
        }
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(queue1.size() == 0){
            int size = queue2.size();
            for (int i = 0; i < size - 1; i++) {
                int tmp = queue2.poll();
                queue1.offer(tmp);
            }
            return queue2.poll();
        }else {
            int size = queue1.size();
            for (int i = 0; i < size - 1; i++) {
                int tmp = queue1.poll();
                queue2.offer(tmp);
            }
            return queue1.poll();
        }
    }

    public int top() {
        if(empty()){
            return -1;
        }
        if(queue1.size() == 0){
            int size = queue2.size();
            for (int i = 0; i < size - 1; i++) {
                int tmp = queue2.poll();
                queue1.offer(tmp);
            }
            int ret = queue2.poll();
            queue1.offer(ret);
            return ret;
        }else {
            int size = queue1.size();
            for (int i = 0; i < size - 1; i++) {
                int tmp = queue1.poll();
                queue2.offer(tmp);
            }
            int ret = queue1.poll();
            queue2.offer(ret);
            return ret;
        }
    }

    public boolean empty() {
        if(queue1.size() == 0 && queue2.size() == 0){
            return true;
        }
        return false;
    }
}
//155 - 最小栈
class MinStack {
    Stack<Integer> stack1;
    Stack<Integer> stack2;
    public MinStack() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int val) {
        stack1.push(val);
        if(stack2.empty()){
            stack2.push(val);
            return;
        }
        if (val <= stack2.peek()){
            stack2.push(val);
        }
    }

    public void pop() {
        if(stack1.empty()){
            return;
        }
        int ret = stack1.pop();
        if(ret == stack2.peek()){
            stack2.pop();
        }
    }

    public int top() {
        if(stack1.empty()){
            return -1;
        }
        return stack1.peek();
    }

    public int getMin() {
        return stack2.peek();
    }
}
public class Test {
}
